import OpusWebSocketServer from './server.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { PORT } from './config.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
//const OUTPUT_DIR = path.join(__dirname, 'output');

const OUTPUT_DIR = '/log/node-opus-ws-server'

// 确保输出目录存在
const absoluteOutputDir = path.resolve(OUTPUT_DIR);
if (!fs.existsSync(absoluteOutputDir)) {
    fs.mkdirSync(absoluteOutputDir, { recursive: true });
    console.log('创建输出目录:', absoluteOutputDir);
}

let server;

async function main() {
    server = new OpusWebSocketServer({
        port: PORT,
        outputDir: absoluteOutputDir
    });

    server.start();
}

main().catch(console.error);

process.on('SIGINT', () => {
    console.log('\n准备关闭服务器...');
    server.stop();
    process.exit(0);
});

process.on('uncaughtException', (err) => {
    console.error('未处理异常:', err);
});
